load("@bazel_skylib//rules:expand_template.bzl", "expand_template")
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")

alias(
    name = "database",
    actual = "@prjtrellis-db",
    visibility = ["//visibility:public"],
)

# Generate version.cpp from version.cpp.in using expand_template
expand_template(
    name = "generate_version",
    out = "libtrellis/generated/version.cpp",
    substitutions = {
        "@CURRENT_GIT_VERSION@": "BAZEL",
    },
    template = "libtrellis/tools/version.cpp.in",
)

CXXOPTS = [
    "-std=c++14",
    "-Wall",
    "-Wextra",
]

DEFINES = [
    "TRELLIS_RPATH_DATADIR=\\\"share\\\"",
    "TRELLIS_PREFIX=\\\"/usr/local\\\"",
    "TRELLIS_PROGRAM_PREFIX=\\\"\\\"",
]

INCLUDES = [
    "libtrellis/include",
    "libtrellis/tools",
    "libtrellis/generated",
]

# Main libtrellis library
cc_library(
    name = "trellis",
    srcs = glob(
        [
            "libtrellis/src/*.cpp",
        ],
        exclude = [
            "libtrellis/src/PyTrellis.cpp",  # Python bindings excluded for now
        ],
    ),
    hdrs = glob([
        "libtrellis/include/*.hpp",
        "libtrellis/include/*.h",
    ]),
    cxxopts = CXXOPTS,
    includes = [
        "libtrellis/include",
    ],
    visibility = ["//visibility:public"],
    deps = [
        "@boost.dll",
        "@boost.filesystem",
        "@boost.program_options",
        "@boost.property_tree",
        "@boost.system",
        "@boost.thread",
    ],
)

# ecpbram tool
cc_binary(
    name = "ecpbram",
    srcs = [
        "libtrellis/tools/ecpbram.cpp",
        "libtrellis/tools/version.hpp",
        "libtrellis/tools/wasmexcept.hpp",
        ":generate_version",
    ],
    cxxopts = CXXOPTS,
    defines = DEFINES,
    includes = INCLUDES,
    visibility = ["//visibility:public"],
    deps = [
        ":trellis",
    ],
)

# ecppack tool
cc_binary(
    name = "ecppack",
    srcs = [
        "libtrellis/tools/ecppack.cpp",
        "libtrellis/tools/version.hpp",
        "libtrellis/tools/wasmexcept.hpp",
        ":generate_version",
    ],
    cxxopts = CXXOPTS,
    defines = DEFINES,
    includes = INCLUDES,
    visibility = ["//visibility:public"],
    deps = [
        ":trellis",
    ],
)

# ecpunpack tool
cc_binary(
    name = "ecpunpack",
    srcs = [
        "libtrellis/tools/ecpunpack.cpp",
        "libtrellis/tools/version.hpp",
        "libtrellis/tools/wasmexcept.hpp",
        ":generate_version",
    ],
    cxxopts = CXXOPTS,
    defines = DEFINES,
    includes = INCLUDES,
    visibility = ["//visibility:public"],
    deps = [
        ":trellis",
    ],
)

# ecppll tool
cc_binary(
    name = "ecppll",
    srcs = [
        "libtrellis/tools/ecppll.cpp",
        "libtrellis/tools/version.hpp",
        "libtrellis/tools/wasmexcept.hpp",
        ":generate_version",
    ],
    cxxopts = CXXOPTS,
    defines = DEFINES,
    includes = INCLUDES,
    visibility = ["//visibility:public"],
    deps = [
        ":trellis",
    ],
)

# ecpmulti tool
cc_binary(
    name = "ecpmulti",
    srcs = [
        "libtrellis/tools/ecpmulti.cpp",
        "libtrellis/tools/version.hpp",
        "libtrellis/tools/wasmexcept.hpp",
        ":generate_version",
    ],
    cxxopts = CXXOPTS,
    defines = DEFINES,
    includes = INCLUDES,
    visibility = ["//visibility:public"],
    deps = [
        ":trellis",
    ],
)
